//
//  ZJGuideController.m
//  Lottery
//
//  Created by ZJ on 2018/8/16.
//  Copyright © 2018年 iCatch Technology Inc. All rights reserved.
//

#import "ZJGuideController.h"
#import "ZJGuideCell.h"
#import "UIView+ZJFrame.h"
#import "ZJTabBarController.h"

@interface ZJGuideController ()

@property (nonatomic, weak) UIImageView *largeImageView;
@property (nonatomic, weak) UIImageView *largeTextImageView;
@property (nonatomic, weak) UIImageView *smallTextImageView;

@property (nonatomic, assign) NSUInteger currentPage; // 用来记录上一次的页数 判断方向

@end

@implementation ZJGuideController

static NSString * const reuseIdentifier = @"Cell";

- (instancetype)init {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    // item大小
    layout.itemSize = kScreenSize.size;
    
    // 间距
    layout.minimumLineSpacing = 0;
    
    // 滑动的方向
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    return [super initWithCollectionViewLayout:layout];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Register cell classes
    // 注册单元格
    [self.collectionView registerClass:[ZJGuideCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    // Do any additional setup after loading the view.
    [self setupGUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupGUI {
    [self setupCollectionView];
    [self setupLine];
    [self setupEnterButton];
}

- (void)setupEnterButton {
    // 添加"立即体验"按钮
    UIButton *enterButton = [[UIButton alloc] init];
    
    [enterButton setBackgroundImage:[UIImage imageNamed:@"guideStart"] forState:UIControlStateNormal];
    [enterButton sizeToFit];
    enterButton.x = 3 * kScreenWidth + (kScreenWidth - enterButton.width) * 0.5;
    enterButton.y = kScreenHeight * 0.9;
    
    [self.collectionView addSubview:enterButton];
    
    // 监听立即体验的点击事件
    [enterButton addTarget:self action:@selector(enterClick) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setupLine {
    // 添加波浪线图片
    UIImage *image = [UIImage imageNamed:@"guideLine"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.x = -220;
    
    [self.collectionView addSubview:imageView];
}

- (void)setupCollectionView {
    // 分页
    self.collectionView.pagingEnabled = YES;
    
    // 取消滚动条
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
     // 取消弹性效果
    self.collectionView.bounces = NO;
    
    // 大图片
    UIImageView *largeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guide1"]];
    
    // 大文字
    UIImageView *largeTextImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideLargeText1"]];
    largeTextImageView.y = kScreenHeight * 0.75;
    
    // 小文字
    UIImageView *smallTextImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guideSmallText1"]];
    smallTextImageView.y = kScreenHeight * 0.85;
    
    // 把图片添加到 collectionView 中
    [self.collectionView addSubview:largeImageView];
    [self.collectionView addSubview:largeTextImageView];
    [self.collectionView addSubview:smallTextImageView];
    
    // 给全局属性赋值
    self.largeImageView = largeImageView;
    self.largeTextImageView = largeTextImageView;
    self.smallTextImageView = smallTextImageView;
}

// 立即体验的按钮
- (void)enterClick {
    // 创建 tabbarController
    ZJTabBarController *vc = [[ZJTabBarController alloc] init];
    
    UIWindow *win = [UIApplication sharedApplication].keyWindow;
    
    [UIView animateWithDuration:0.25 animations:^{
        // 切换 window 的根控制器
        win.rootViewController = vc;
    }];
}

// 监听 collectionView 被拖拽完成(当已上最后一页再向右拖拽时进入app主页)
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    // 获取到scrollView 的偏移量
    CGFloat offsetX = scrollView.contentOffset.x;
    
    // 获取页数
    int page = offsetX / kScreenWidth;
    
    if (page >= 3) {
        [self enterClick];
    }
}

// 监听 collectionView 滑动完成(scrollView减速完成)
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // 获取到scrollView 的偏移量
    CGFloat offsetX = scrollView.contentOffset.x;
    
    // 获取页数
    int page = offsetX / kScreenWidth;
    
    int index = page + 1;
    
    // 获取图片的名字
    NSString *largeImageName = [NSString stringWithFormat:@"guide%d", index];
    NSString *largeTextImageName = [NSString stringWithFormat:@"guideLargeText%d", index];
    NSString *smallTextImageName = [NSString stringWithFormat:@"guideSmallText%d", index];
    
    // 更换图片
    self.largeImageView.image = [UIImage imageNamed:largeImageName];
    self.largeTextImageView.image = [UIImage imageNamed:largeTextImageName];
    self.smallTextImageView.image = [UIImage imageNamed:smallTextImageName];
    
    CGFloat originPoint = offsetX + kScreenWidth;
    
    // 判断方向
    if (self.currentPage > page) {
        // 在做动画之前 先改变图片的 x
        // 从左往右
        originPoint = offsetX - kScreenWidth;
    }
    
    self.largeImageView.x = originPoint;
    self.largeTextImageView.x = originPoint;
    self.smallTextImageView.x = originPoint;
    
    // 加特技
    [UIView animateWithDuration:0.25
                     animations:^{
                         // 把介绍图片的 x 变到 偏移量的位置
                         self.largeImageView.x = offsetX;
                         self.largeTextImageView.x = offsetX;
                         self.smallTextImageView.x = offsetX;
                     }];
    
    self.currentPage = page;
}

#pragma mark <UICollectionViewDataSource>
// 有多少组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

// 某一组有多少行
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 4;
}

// cell样子
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    // 去缓存池找
    ZJGuideCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    // Configure the cell
    // 获取图片
    NSString *imageName = [NSString stringWithFormat:@"guide%zdBackground", indexPath.row + 1];
    
    // 把数据(图片, 或者可能是模型 可能是数组 可能是字典)传给cell
    cell.image = [UIImage imageNamed:imageName];
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
